/**
 *  API文档 V2 
 *  http://open.weibo.com/wiki/API%E6%96%87%E6%A1%A3_V2
 * 
 *  Sina Weibo Flash SDK 
 *  http://code.google.com/p/weibosdk/
 * 
 */ 


import com.sina.microblog.MicroBlog;
import com.sina.microblog.data.*;
import com.sina.microblog.events.*;
import com.sina.microblog.utils.StringEncoders;

import flash.events.MouseEvent;

import mx.controls.Alert;

import com.yf.webtest.Test;


private var _mb:MicroBlog;

private function main():void
{
	loginBtn.addEventListener(MouseEvent.CLICK, loginBtnHandler);
	
	_mb = new MicroBlog();
	_mb.consumerKey = "3812612806"; //申请的App Key
	_mb.consumerSecret = "cf4e59c72d670420697baa5727a09664"; //申请的App Secrect
	_mb.proxyURI = "http://oauth2webtest.sinaapp.com/proxy.php"; //写你自己部署的代理接口地址
}
	

private function loginBtnHandler(event:MouseEvent):void
{
	_mb.addEventListener(MicroBlogEvent.LOGIN_RESULT, onLoginResult);
	_mb.addEventListener(MicroBlogErrorEvent.LOGIN_ERROR, onLoginErrorResult);
	_mb.login();

}


/*
* 成功登录，获取到access_token，expires_in和refresh_token
*/
private function onLoginResult(e:MicroBlogEvent):void
{
	_mb.removeEventListener(MicroBlogEvent.LOGIN_RESULT, onLoginResult);
	trace(e.result["access_token"] + "::" + e.result["expires_in"] + "::" + e.result["refresh_token"]);  
	Alert.show(e.result["access_token"] + "::" + e.result["expires_in"] + "::" + e.result["refresh_token"]);
	
	testAPI();
}

private function onLoginErrorResult(event:MicroBlogErrorEvent):void
{
	trace("onLoginErrorResult : "+event.message);
	Alert.show("onLoginErrorResult : "+event.message);
}



/**
 * 测试通过调用callWeiboAPI通用接口调用开放平台接口：“2/account/get_uid”
 */
private function testAPI():void
{
	_mb.addEventListener("callWeiboApiResult", callWeiboApiResult);
	_mb.addEventListener("callWeiboApiError", callWeiboApiError);
	_mb.callWeiboAPI("2/account/get_uid", null, "GET", "callWeiboApiResult", "callWeiboApiError"); 
	//注意，这里使用的callWeiboApiResult和callWeiboApiError字符串代表了两个自定义事件，你也可以使用自己定义的字符串区分各个api的调用。
}

/**
 * 成功调用api
 */
private function callWeiboApiResult(e:MicroBlogEvent):void
{
	trace(e.result.uid); //按照线上文档返回格式格式： {"uid":"3456676543"}
	Alert.show("成功调用api:"+e.result.uid);
	
	//关注id列表
	_mb.addEventListener("f", f);
	_mb.addEventListener("fe", fe);
	_mb.callWeiboAPI("2/friendships/friends/ids", null, "GET", "f", "fe"); 
}

/**
 * 调用api失败，获取服务器返回的错误信息
 */
private function callWeiboApiError(e:MicroBlogErrorEvent):void
{
	trace("callWeiboApiError:"+e.message);
	Alert.show("callWeiboApiError:"+e.message);
}






private function f(e:MicroBlogEvent):void
{
	/*
	for(var i in e.result)
	{
		Test.t(i+" : "+e.result[i]);
	}
	*/
	
	Test.t(e.result.ids);
}
private function fe(e:MicroBlogErrorEvent):void
{
	Alert.show(e.message);
}












